package  
{
	import org.flixel.FlxState;
	import registry.Resources;
	import org.flixel.*;
	/**
	 * ...
	 * @author The Greatest Gatsby
	 */
	public class PlayState extends FlxState
	{
		public var levelNum:int;
		public function PlayState(lvlNum:int):void{levelNum = lvlNum;}
		
		public var currentLevel:Level;
		public var levelFirst:Level_first;
		public var levelSecond:Level_second;
		public var levelThird:Level_third;
		public var levelFourth:Level_fourth;
		public var levelFifth:Level_fifth;
		public var levelSixth:Level_sixth;
		public var levelSeventh:Level_seventh;
		public var levelEighth:Level_eighth;
		public var player:ColoredSprite;
		public var baddy:Enemy;
		public var keySelected:ColoredSprite;
		public var keyQUI:ColoredSprite;
		public var keyQText:FlxText;
		public var keyWUI:ColoredSprite;
		public var keyWText:FlxText;
		public var keyEUI:ColoredSprite;
		public var keyEText:FlxText;
		public var blue:uint;
		public var green:uint;
		public var yellow:uint;
		public var blueEmitter:FlxEmitter;
		public var yellowEmitter:FlxEmitter;
		public var greenEmitter:FlxEmitter;
		public var currentEmitter:FlxEmitter;
		public var blueExploder:FlxEmitter;
		public var yellowExploder:FlxEmitter;
		public var greenExploder:FlxEmitter;
		public var explosionEmitter:FlxEmitter;
		public var explosionTimer:int = 1;
		public var explosionSize:int = 20;
		private var bluePixel:FlxParticle;
		private var greenPixel:FlxParticle;
		private var yellowPixel:FlxParticle;
		[Embed(source = '../res/blueSprites.png')] private var playerBlue:Class;
		[Embed(source = '../res/greenSprites.png')] private var playerGreen:Class;
		[Embed(source = '../res/yellowSprites.png')] private var playerYellow:Class;
		public var musicPlaying:Boolean;
		[Embed(source = '../res/gameplay_music.mp3')] private var gameplay_music:Class;
		[Embed(source = '../res/change_color.mp3')] private var change_color:Class;
		[Embed(source = '../res/death.mp3')] private var death:Class;
		[Embed(source = '../res/jump.mp3')] private var jump:Class;
		private var jumping:Boolean;
		
		public override function create():void
		{
			FlxG.bgColor = 0xff000000;
			blue = 0xff3399FF;
			green = 0xff00CC00;
			yellow = 0xffFFFF00;
				
			//Create player
			player = new ColoredSprite(FlxG.width / 5, 0);
			
			
			
			player.addAnimation("Walk", [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 25, true);
			player.addAnimation("Stand", [21, 22, 23, 24], 5, true);
			player.addAnimation("Takeoff", [0], 1, true);
			player.addAnimation("Jumping", [1, 2, 3, 4, 5, 6], 5, false);
			player.addAnimation("Landing", [7, 8, 9], 1, false);
			jumping = true;
			player.width = 12;
			player.height = 12;
			player.maxVelocity.x = 150;
			player.maxVelocity.y = 200;
			player.acceleration.y = 200;
			player.drag.x = player.maxVelocity.x * 4;
			player.gameplayColor.setBlue();
			if (levelNum == 1)
				player.gameplayColor.setGreen();
				
			if (levelNum == 1)
			{
				levelFourth = new Level_fourth();
				currentLevel = levelFourth
				player.loadGraphic(playerGreen, true, true, 25, 24);
			}
			else if (levelNum == 2)
			{
				levelFirst = new Level_first();
				currentLevel = levelFirst;
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}
			else if (levelNum == 3)
			{
				levelSecond = new Level_second();
				currentLevel = levelSecond;
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}
			else if (levelNum == 4)
			{
				levelThird = new Level_third();
				currentLevel = levelThird
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}	
			else if (levelNum == 5)
			{
				levelFifth = new Level_fifth();
				currentLevel = levelFifth
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}
			else if (levelNum == 6)
			{
				levelSixth = new Level_sixth();
				currentLevel = levelSixth
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}
			else if (levelNum == 7)
			{
				levelSeventh = new Level_seventh();
				currentLevel = levelSeventh
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}
			else if (levelNum == 8)
			{
				levelEighth = new Level_eighth();
				currentLevel = levelEighth
				player.loadGraphic(playerBlue, true, true, 25, 24);
			}
			
			baddy = new Enemy(FlxG.width / 5-50);
			baddy.maxVelocity.x = 150;
			baddy.maxVelocity.y = 200;
			baddy.acceleration.y = 200;
			baddy.drag.x = player.maxVelocity.x * 4;
			baddy.gameplayColor.setGreen();
			
			//add(baddy);
			
			FlxG.camera.focusOn(player.origin);
			FlxG.camera.update();
			blueEmitter = new FlxEmitter(player.getMidpoint().x, player.getMidpoint().y,100);
			for (var i:int = 0; i < blueEmitter.maxSize/2; i++) {
				bluePixel = new FlxParticle();
				bluePixel.makeGraphic(2, 2, blue);
				bluePixel.visible = false; //Make sure the particle doesn't show up at (0, 0)
				blueEmitter.add(bluePixel);
				bluePixel = new FlxParticle();
				bluePixel.makeGraphic(1, 1, blue);
				bluePixel.visible = false;
				blueEmitter.add(bluePixel);
			}
			blueEmitter.start(false, 0, .1, 0);
			blueEmitter.setXSpeed( -0, 0);
			blueEmitter.setYSpeed( -0, 0);
			blueEmitter.visible = true;
			add(blueEmitter);
			
			yellowEmitter = new FlxEmitter(player.getMidpoint().x, player.getMidpoint().y,100);
			for (var i:int = 0; i < yellowEmitter.maxSize/2; i++) {
				yellowPixel = new FlxParticle();
				yellowPixel.makeGraphic(2, 2, yellow);
				yellowPixel.visible = false; //Make sure the particle doesn't show up at (0, 0)
				yellowEmitter.add(yellowPixel);
				yellowPixel = new FlxParticle();
				yellowPixel.makeGraphic(1, 1, yellow);
				yellowPixel.visible = false;
				yellowEmitter.add(yellowPixel);
			}
			yellowEmitter.setXSpeed( -0,0);
			yellowEmitter.setYSpeed( -0, 0);
			yellowEmitter.visible = true;
			add(yellowEmitter);
			
			greenEmitter = new FlxEmitter(player.getMidpoint().x, player.getMidpoint().y,100);
			for (var i:int = 0; i < greenEmitter.maxSize/2; i++) {
				greenPixel = new FlxParticle();
				greenPixel.makeGraphic(2, 2, green);
				greenPixel.visible = false; //Make sure the particle doesn't show up at (0, 0)
				greenEmitter.add(greenPixel);
				greenPixel = new FlxParticle();
				greenPixel.makeGraphic(1, 1, green);
				greenPixel.visible = false;
				greenEmitter.add(greenPixel);
			}
			greenEmitter.setXSpeed( -0, 0);
			greenEmitter.setYSpeed( -0, 0);
			greenEmitter.visible = true;
			add(greenEmitter);
			
			blueExploder = new FlxEmitter(player.getMidpoint().x, player.getMidpoint().y, 100);
			blueExploder.add(bluePixel);
			for (var i:int = 0; i < blueEmitter.maxSize/2; i++) {
				bluePixel = new FlxParticle();
				bluePixel.makeGraphic(2, 2, blue);
				bluePixel.visible = false; //Make sure the particle doesn't show up at (0, 0)
				blueExploder.add(bluePixel);
				bluePixel = new FlxParticle();
				bluePixel.makeGraphic(1, 1, blue);
				bluePixel.visible = false;
				blueExploder.add(bluePixel);
			}
			blueExploder.minParticleSpeed = new FlxPoint(-100, -100);
			blueExploder.maxParticleSpeed = new FlxPoint(100, 100);
			blueExploder.visible = true;
			add(blueExploder);
			
			yellowExploder = new FlxEmitter(player.getMidpoint().x, player.getMidpoint().y, 100);
			yellowExploder.add(yellowPixel);
			for (var i:int = 0; i < yellowEmitter.maxSize/2; i++) {
				yellowPixel = new FlxParticle();
				yellowPixel.makeGraphic(2, 2, yellow);
				yellowPixel.visible = false; //Make sure the particle doesn't show up at (0, 0)
				yellowExploder.add(yellowPixel);
				yellowPixel = new FlxParticle();
				yellowPixel.makeGraphic(1, 1, yellow);
				yellowPixel.visible = false;
				yellowExploder.add(yellowPixel);
			}
			yellowExploder.minParticleSpeed = new FlxPoint(-100, -100);
			yellowExploder.maxParticleSpeed = new FlxPoint(100, 100);
			yellowExploder.visible = true;
			add(yellowExploder);
			
			greenExploder = new FlxEmitter(player.getMidpoint().x, player.getMidpoint().y, 100);
			greenExploder.add(greenPixel);
			for (var i:int = 0; i < greenEmitter.maxSize/2; i++) {
				greenPixel = new FlxParticle();
				greenPixel.makeGraphic(2, 2, green);
				greenPixel.visible = false; //Make sure the particle doesn't show up at (0, 0)
				greenExploder.add(greenPixel);
				greenPixel = new FlxParticle();
				greenPixel.makeGraphic(1, 1, green);
				greenPixel.visible = false;
				greenExploder.add(greenPixel);
			}
			greenExploder.minParticleSpeed = new FlxPoint(-100, -100);
			greenExploder.maxParticleSpeed = new FlxPoint(100, 100);
			greenExploder.visible = true;
			add(greenExploder);
			
			
			currentEmitter = blueEmitter;
			explosionEmitter = blueExploder;
			if (levelNum == 1)
			{
				currentEmitter.on = false;
				currentEmitter = greenEmitter;
				currentEmitter.start(false, 0, .1, 0);
				player.gameplayColor.setGreen();
			}
			
			add(player);
			FlxG.camera.follow(player);
			
			//UI
			keySelected = new ColoredSprite();
			keySelected.makeGraphic(51, 51, 0xFFFF0000);
			keySelected.scrollFactor.x = keySelected.scrollFactor.y = 0;
			add(keySelected);
			
			keyQUI = new ColoredSprite(10, 10);
			keyQUI.makeGraphic(41, 41, green);
			keyQUI.scrollFactor.x = keyQUI.scrollFactor.y = 0;
			keyQText = new FlxText(0, 0, FlxG.width, "Q");
			keyQText.setFormat (null, 28, 0xFF000000, "left");
			keyQText.x = keyQUI.x + 8;
			keyQText.y = keyQUI.y + 1;
			keyQText.scrollFactor.x = keyQText.scrollFactor.y = 0;
			add(keyQUI);
			add(keyQText);
			
			keyWUI = new ColoredSprite(65, 10);
			keyWUI.makeGraphic(41, 41, blue);
			keyWUI.scrollFactor.x = keyWUI.scrollFactor.y = 0;
			keyWText = new FlxText(0, 0, FlxG.width, "W");
			keyWText.setFormat (null, 28, 0xFF000000, "left");
			keyWText.x = keyWUI.x + 6;
			keyWText.y = keyWUI.y + 3;
			keyWText.scrollFactor.x = keyWText.scrollFactor.y = 0;
			add(keyWUI);
			add(keyWText);
			
			keyEUI = new ColoredSprite(120, 10);
			keyEUI.makeGraphic(41, 41, yellow);
			keyEUI.scrollFactor.x = keyEUI.scrollFactor.y = 0;
			keyEText = new FlxText(0, 0, FlxG.width, "E");
			keyEText.setFormat (null, 28, 0xFF000000, "left");
			keyEText.x = keyEUI.x + 9;
			keyEText.y = keyEUI.y + 3;
			keyEText.scrollFactor.x = keyEText.scrollFactor.y = 0;
			add(keyEUI);
			add(keyEText);
			
			keySelected.x = keyEUI.x - 5;
			keySelected.y = keyEUI.y - 5;
			
			if (player.gameplayColor.color == 0)
			{
				keySelected.x = keyQUI.x - 5;
				keySelected.y = keyQUI.y - 5;
			}
			else if (player.gameplayColor.color == 1)
			{
				keySelected.x = keyWUI.x - 5;
				keySelected.y = keyWUI.y - 5;
			}
			else if (player.gameplayColor.color == 2)
			{
				keySelected.x = keyEUI.x - 5;
				keySelected.y = keyEUI.y - 5;
			}
			
			
			FlxG.play(gameplay_music, 1, true);
		}
		
		override public function update():void
		{
			FlxG.camera.update();
			player.acceleration.x = 0;
			currentEmitter.x = player.getMidpoint().x;
			currentEmitter.y = player.getMidpoint().y;

			
			if (FlxG.keys.LEFT)
			{
				player.acceleration.x = -player.maxVelocity.x * 8;
				player.facing = FlxObject.LEFT;
				if(player.isTouching(FlxObject.FLOOR))
					player.play("Walk");
			}
			else if (FlxG.keys.RIGHT)
			{
				player.acceleration.x = player.maxVelocity.x * 8;
				player.facing = FlxObject.RIGHT;
				if(player.isTouching(FlxObject.FLOOR))
					player.play("Walk");
			}
			else if(!player.isTouching(FlxObject.FLOOR))
			{
				player.play("Jumping");
				jumping = true;
			}
			else if(!player.play("Landing"))
			{
				player.play("Stand");
			}
			
			if (player.isTouching(FlxObject.FLOOR) && jumping)
			{
				player.play("Landing")
				jumping = false;
			}
			
			if (FlxG.keys.SPACE && player.isTouching(FlxObject.FLOOR) && levelNum != 7)
			{
				player.velocity.y = -player.maxVelocity.y / 2;
				player.play("Takeoff");
			}
			if (player.y > 600)
			{
				FlxG.switchState(new PlayState(levelNum));
			}
				
			if (FlxG.keys.Q)
			{
				currentEmitter.on = false;
				currentEmitter = greenEmitter;
				currentEmitter.start(false, 0, .1, 0);
				if (player.gameplayColor.color != 0)
				{
					FlxG.play(change_color, 0.5, false, true);
					explosionEmitter = greenExploder;
					explosionEmitter.x = player.getMidpoint().x;
					explosionEmitter.y = player.getMidpoint().y;
					explosionEmitter.start(true, explosionTimer, 0.1, explosionSize);
				}
				player.gameplayColor.setGreen();
				player.loadGraphic(playerGreen, true, true, 25, 24);
				keySelected.x = keyQUI.x - 5;
				keySelected.y = keyQUI.y - 5;
			}
			else if (FlxG.keys.W)
			{
				currentEmitter.on = false;
				currentEmitter = blueEmitter;
				currentEmitter.start(false, 0, .1, 0);
				if (player.gameplayColor.color != 1)
				{
					FlxG.play(change_color, 0.5, false, true);
					explosionEmitter = blueExploder;
					explosionEmitter.x = player.getMidpoint().x;
					explosionEmitter.y = player.getMidpoint().y;
					explosionEmitter.start(true, explosionTimer, 0.1, explosionSize);
				}
				player.gameplayColor.setBlue();
				player.loadGraphic(playerBlue, true, true, 25, 24);
				keySelected.x = keyWUI.x - 5;
				keySelected.y = keyWUI.y - 5;
			}
			else if (FlxG.keys.E)
			{
				currentEmitter.on = false;
				currentEmitter = yellowEmitter;
				currentEmitter.start(false, 0, .1, 0);
				if (player.gameplayColor.color != 2)
				{
					FlxG.play(change_color, 0.5, false, true);
					explosionEmitter = yellowExploder;
					explosionEmitter.x = player.getMidpoint().x;
					explosionEmitter.y = player.getMidpoint().y;
					explosionEmitter.start(true, explosionTimer, 0.1, explosionSize);
				}
				player.gameplayColor.setYellow();
				player.loadGraphic(playerYellow, true, true, 25, 24);
				keySelected.x = keyEUI.x - 5;
				keySelected.y = keyEUI.y - 5;
			}
			
			FlxG.worldBounds = new FlxRect(player.x, player.y, player.width, player.height);
			
			super.update();
						
			if (player.gameplayColor.color == 1)
			{
				FlxG.collide(currentLevel.layerGroup1BlueLayer, player);
			}
			else if (player.gameplayColor.color == 0)
			{
				FlxG.collide(currentLevel.layerGroup1GreenLayer, player);
			}
			else if (player.gameplayColor.color == 2)
			{
				FlxG.collide(currentLevel.layerGroup1YellowLayer, player);
			}
			
			player.draw();
					
			if (FlxG.overlap(player, baddy))
			{
				if (baddy.gameplayColor.color == player.gameplayColor.color)
				{
					FlxG.switchState(new PlayState(levelNum));
				}
					
			}
							
			if (FlxG.collide(player, currentLevel.layerGroup1WinLayer))
			{
				levelNum++;
				if (levelNum == 9)
					FlxG.switchState(new MenuState());
				else
					FlxG.switchState(new PlayState(levelNum));
			}
			
			FlxG.worldBounds = new FlxRect(baddy.x, baddy.y, baddy.width, baddy.height);
			
			//FlxG.collide(level.layerGroup1GreenLayer, baddy);
		}
		
	}

}